using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace StylishNumbersList
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();

			AddFilter("700");
			AddFilter("707");
			AddFilter("770");
			AddFilter("777");

			//AddFilter("70000");
			////AddFilter("70007");
			//AddFilter("77077");
			//AddFilter("70707");
			//AddFilter("77700");

			richTextBox1.Text = GetStylishNumberList();
		}

		public string GetStylishNumberList()
		{
			StringBuilder sb = new StringBuilder();
			int count = 0;
			foreach (int i in StylishNumbers())
			{
				sb.AppendLine(i.ToString());
				count++;
			}
			this.Text = count.ToString();
			return sb.ToString();
		}

		public IEnumerable<int> StylishNumbers()
		{
			foreach (int i in AllNumbers())
			{
				if (IsStylish(i))
				{
					yield return i;
				}
			}
		}

		public bool IsStylish(int number)
		{
			Digits = GetDigits(number);
			DigitsString = number.ToString();
			return 
				(NonZeroAreEqual()
				&& MatchesFilters(DigitsString))
				|| Digits[0] == Digits[2];
		}

		private List<Regex> filters = new List<Regex>();
		public void AddFilter(string s)
		{
			filters.Add(new Regex(s.Replace("7", "[1-9]")));
		}

		public bool MatchesFilters(string s)
		{
			foreach (Regex r in filters)
			{
				if (r.IsMatch(s))
				{
					return true;
				}
			}
			return false;
		}

		int[] Digits;
		string DigitsString;

		public bool NonZeroAreEqual()
		{
			int digit = -1;
			foreach (int i in Digits)
			{
				if (i != 0)
				{
					if (digit == -1)
					{
						digit = i;
					}
					else
					{
						if (i != digit)
						{
							return false;
						}
					}
				}
			}
			return true;
		}

		public int[] GetDigits(int number)
		{
			List<int> digits = new List<int>();
			while (number > 0)
			{
				int residual = number % 10;
				digits.Insert(0, residual);
				number = (number - residual) / 10;
			}
			return digits.ToArray();
		}

		public IEnumerable<int> AllNumbers()
		{
			return GetRange(100, 999);
		}

		public IEnumerable<int> GetRange(int start, int end)
		{
			for (int i = start; i < end; i++)
			{
				yield return i;
			}
		}
	}
}